import { Component, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OutsourcingWorkFlowServiceProxy, WorkflowServiceProxy, OutsouringServiceProxy, UserServiceProxy, ProcessingOrderServiceProxy, ProcessingForListViewDto, TaskServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { FlowworkauditDetailModelComponent } from '../flowworkauditDetailModel/flowworkauditDetailModel.component';
import { ProcessingOrderListComponent } from '../ProcessingOrderList/ProcessingOrderList.component';
import { ResourceLoader } from '@angular/compiler';

@Component({
    selector: 'ProcessingOrderRecordList',
    templateUrl: './ProcessingOrderRecordList.component.html',
    styleUrls: ['./ProcessingOrderRecordList.component.css']
})
export class ProcessingOrderRecordListComponent extends AppComponentBase {
    @ViewChild('flowworkauditdetailmodel') flowworkauditdetailmodel: FlowworkauditDetailModelComponent;
    // @ViewChild('processingOrderList') processingOrderList: ProcessingOrderListComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    filterText = '';
    advancedFiltersAreShown = false;

    startDate: any;
    endDate: any;
    outsourcings: ProcessingForListViewDto[];
    constructor(injector: Injector,
        private _outsourcingWorkFlowServiceProxy: OutsourcingWorkFlowServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _outsourcingServiceProxy: OutsouringServiceProxy,
        private _processOrderServiceProxy: ProcessingOrderServiceProxy,
        private _taskServiceProxy: TaskServiceProxy,
        private _filedownloadService: FileDownloadService,
        private _workflowServiceProxy: WorkflowServiceProxy) {

        super(injector);
    }

    getOutsourcingAgreedList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._outsourcingWorkFlowServiceProxy.getProcessingForList(
            this.startDate,
            this.endDate,
            this.filterText,
            'code',
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event),
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            console.log(result);
            this.outsourcings = result.items;
            this.primengTableHelper.hideLoadingIndicator();

            _.forEach(result.items, viewItem => {
                this._outsourcingWorkFlowServiceProxy.getOutsourcingByRequisitionCode(viewItem.requisitionCode).subscribe(result => {
                    viewItem.applyStatus = result.applyStatus;
                    viewItem.outsourcingType = result.outsourcingType;
                    viewItem.workflowId = result.workflowId;
                });
                this._outsourcingWorkFlowServiceProxy.getProcessingDeliveryByProcessingId(viewItem.processingOrderId, viewItem.taskForModelId).subscribe(result => {
                    viewItem.deliveredAmount = result.deliveredAmount;
                    viewItem.passedAmount = result.passedAmount;
                    viewItem.noPassedAmount = result.noPassedAmount;
                    viewItem.quantityAmount = result.quantityAmount;
                    viewItem.isOverdue = result.isOverdue;
                    viewItem.overdueDay = result.overdueDay;
                });

            });
        });
    }
    Detail(records): void {
        let workflowName = records.outsourcingType === 0 ? '一般过程外协申请' : '关键过程外协申请';

        this.flowworkauditdetailmodel.show(workflowName, records.workflowId);
    }
    OpenProcesingList(code: string): void {
        // this.processingOrderList.show(code);
    }
    exportToexcel(event?: LazyLoadEvent): void {
        this._outsourcingWorkFlowServiceProxy.getProcessingForListToExecl(
            this.filterText,
            this.startDate,
            this.endDate
            ).subscribe(dto => {
            this._filedownloadService.downloadTempFile(dto);
        });
    }
}
